package ma.controller.panels.parameterpanels;

import ma.controller.panels.auxillary.CtrInformationPanel;
import ma.model.MWGDEInterface;
import ma.model.ManufacturingMethodInterface;
import ma.model.auxillary.Parameter;
import ma.view.gui.parameterPanels.mwgde.ProcessPanelDrilling;
import ma.view.gui.parameterPanels.mwgde.ProcessPanelMilling;
import ma.view.gui.parameterPanels.mwgde.ProcessPanelTurning;

public class CtrProcessPanelMWGDE extends
        CtrProcessPanel {
    
    public CtrProcessPanelMWGDE(
            ManufacturingMethodInterface manuMethod,
            CtrInformationPanel infoPanelCtr) {
        super(manuMethod, infoPanelCtr);        
    }

    @Override
    /*
     * (non-Javadoc)
     * @see ma.controller.panels2.parameterpanels.CtrParameterPanel#init()
     */
    protected void init() {        
        for (Parameter param : manuMethod.getProcessParameters()) {
            /*
             * What happens here is to make sure that the text fields for v_f
             * and f get created. With the standard init function one of them
             * wouldn't get created if a user has set one of the other
             * variables, saved the state and then recreates the process
             * parameter panel. Because then one of the two variables'
             * dependency is set to "true" and it won't get created.
             */
            boolean dependenceWasSet = false;
            if(param.getDependence() && param.getAbbreviation().equals("v_f") ||
                    param.getDependence() && param.getAbbreviation().equals("f")) {
                param.setDependence(false);
                dependenceWasSet = true;
            }
            
            if(!param.getDependence()) {
                String[] paramInfo = new String[5];
                paramInfo[0] = param.getName();
                paramInfo[1] = param.getAbbreviation();
                paramInfo[2] = param.getGuiAbbreviation();
                paramInfo[3] = param.getUnit();
                Double paramValue = param.getValue();
                if(paramValue == null) {
                    paramInfo[4] = "";
                } else if (paramValue.equals(Double.NaN)) {
                    paramInfo[4] = "";    
                } else {
                    paramInfo[4] = paramInfo[4] = String.valueOf(paramValue);
                }                
                panel.addParameterPanel(paramInfo);
            }
            
            if((param.getAbbreviation().equals("v_f") ||
                    param.getAbbreviation().equals("f")) && dependenceWasSet) {
                param.setDependence(true);
            }
            dependenceWasSet = false;
        }
        panel.update();
    }
    
    @Override
    /*
     * (non-Javadoc)
     * @see ma.controller.panels2.parameterpanels.CtrParameterPanel#createView()
     */
    protected void createView() {
        MWGDEInterface manuMethod = (MWGDEInterface) this.manuMethod;
        switch(manuMethod.getSubType()) {
        case TURNING:
            panel = new ProcessPanelTurning(
                    manuMethod.getName(), manuMethod, infoPanelCtr);
            break;
        case DRILLING:
            panel = new ProcessPanelDrilling(
                    manuMethod.getName(), manuMethod, infoPanelCtr);
            break;
        case MILLING:
            panel = new ProcessPanelMilling(
                    manuMethod.getName(), manuMethod, infoPanelCtr);
            break;
        default:
            break;
        }        
    }
}
